﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Collections;

public partial class StandExpenses : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            txtmonth.Text = DateTime.Now.ToString("yyyy-MM-01");
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = "";
        if (txtshop.Text != "")
        {
            str += " and (shopcode like '%" + txtshop.Text + "%' or shopname like '%" + txtshop.Text + "%')";
        }
        if (txtmonth.Text != "" && Convert.ToDateTime(txtmonth.Text) != Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            str += " and convert(varchar(20),PayMonth,23) like '" + Convert.ToDateTime(txtmonth.Text).ToString("yyyy-MM") + "%'";
        }
        string sql = @"select * from Bas_Stand_Expenses where 1=1 " + str;
        QueryData(this.Store1, mid, sql);
    }
    protected void BtnDeletData(object sender, DirectEventArgs e)
    {
        if (txtmonth.Text == "" || Convert.ToDateTime(txtmonth.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请选择删除日期！");
            return;
        }
        string sql = "delete from Bas_Stand_Expenses where convert(varchar(20),PayMonth,23) like '" + Convert.ToDateTime(txtmonth.Text).ToString("yyyy-MM") + "%'";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("删除成功！");
            BtnQureyUser(null, null);
        }
        else
        {
            ShowBox("删除失败！");
        }
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    protected void sheetnameselect(object sender, DirectEventArgs e)
    {
        string sheetname = e.ExtraParams["sheetname"].ToString();
        InsertData(this.Hidden1.Value.ToString(), sheetname);
        this.Window1.Hide();
    }
    protected void MultiUpload1_FileUpload(object sender, FileUploadEventArgs e)
    {
        //System.Threading.Thread.Sleep(3000); // Just for testing
        string path = Server.MapPath("../UploadFile/") + e.FileName;
        this.Hidden1.Value = e.FileName;
        e.PostedFile.SaveAs(path);
        ArrayList al = ExcelSheetName(path);
        string sheetname = "";
        if (al.Count == 1)
        {
            sheetname = al[0].ToString();
            InsertData(e.FileName, sheetname);
        }
        else
        {
            string str = "";
            foreach (string s in al)
            {
                str += " select '" + s + "' as sheetname union all ";
            }
            this.Store4.DataSource = mydb.DM_GetDateSet(str.Substring(1,str.Length-11));
            this.Store4.DataBind();
            this.Window1.Show();
        }
    }

    private void InsertData(string FileName,string sheetname)
    {
        string path = Server.MapPath("../UploadFile/") + FileName;
        DataSet ds = ExcelDataSource(path, sheetname);
        string sql = "";
        try
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                sql += @" insert into Bas_Stand_Expenses
                      values(newid(),'" + dr[0] + "','"
                                        + dr[1] + "','"
                                        + dr[2] + "','"
                                        + dr[3] + "','"
                                        + dr[4] + "','"
                                        + dr[5] + "','"
                                        + dr[6] + "','"
                                        + dr[7] + "','"
                                        + dr[8] + "','"
                                        + dr[9] + "','"
                                        + dr[10] + "','"
                                        + dr[11] + "','"
                                        + dr[12] + "','"
                                        + Session["UserCode"] + "',getdate()"
                                        + ") ";
            }
            if (mydb.DM_ExecuteNonQuery(sql) != -1)
            {
                BtnQureyUser(null, null);
                X.Msg.Notify("导入成功！", "文件名: " + FileName).Show();
            }
            else
            {
                X.Msg.Notify("导入失败！", "文件名: " + FileName).Show();
            }
        }
        catch (Exception ex)
        {
            ShowBox(ex.Message);
        }
    }
}